﻿#include "GuildData.h"

GuildRank::GuildRank()
{
}

GuildRank::GuildRank(BinaryReader *reader, bool Offline)
{
    Name = reader->ReadString();
    Options = static_cast<GuildRankOptions>(reader->ReadByte());
    if (!Offline)
    {
        Index = reader->ReadInt32();
    }
    int Membercount = reader->ReadInt32();
    for (int j = 0; j < Membercount; j++)
    {
        GuildMember tempVar(reader, Offline);
        Members.push_back(&tempVar);
    }
}

void GuildRank::Save(BinaryWriter *writer, bool Save)
{
    writer->Write(Name);
    writer->Write(static_cast<unsigned char>(Options));
    if (!Save)
    {
        writer->Write(Index);
    }
    writer->Write(Members.size());
    for (int j = 0; j < Members.size(); j++)
    {
        Members[j]->Save(writer);
    }
}

GuildStorageItem::GuildStorageItem()
{
}

GuildStorageItem::GuildStorageItem(BinaryReader *reader)
{
    Item = new UserItem(reader);
    UserId = reader->ReadInt64();
}

void GuildStorageItem::Save(BinaryWriter *writer)
{
    Item->Save(writer);
    writer->Write(UserId);
}

GuildMember::GuildMember()
{
}

GuildMember::GuildMember(BinaryReader *reader, bool offline)
{
    name = reader->ReadString();
    Id = reader->ReadInt32();
    LastLogin = DateTime::FromBinary(reader->ReadInt64());
    hasvoted = reader->ReadBoolean();
    Online = reader->ReadBoolean();
    Online = offline ? false : Online;
}

void GuildMember::Save(BinaryWriter *writer)
{
    writer->Write(name);
    writer->Write(Id);
    writer->Write(LastLogin.ToBinary());
    writer->Write(hasvoted);
    writer->Write(Online);
}

GuildBuffInfo::GuildBuffInfo()
{
    Stats = new Stats();
}

GuildBuffInfo::GuildBuffInfo(BinaryReader *reader)
{
    Id = reader->ReadInt32();
    Icon = reader->ReadInt32();
    name = reader->ReadString();
    LevelRequirement = reader->ReadByte();
    PointsRequirement = reader->ReadByte();
    TimeLimit = reader->ReadInt32();
    ActivationCost = reader->ReadInt32();

    Stats = new Stats(reader);
}

GuildBuffInfo::GuildBuffInfo(InIReader *reader, int i)
{
    Id = reader->ReadInt32("Buff-" + std::to_string(i), "Id", 0);
    Icon = reader->ReadInt32("Buff-" + std::to_string(i), "Icon", 0);
    name = reader->ReadString("Buff-" + std::to_string(i), "Name", "");
    LevelRequirement = reader->ReadByte("Buff-" + std::to_string(i), "LevelReq", 0);
    PointsRequirement = reader->ReadByte("Buff-" + std::to_string(i), "PointsReq", 1);
    TimeLimit = reader->ReadInt32("Buff-" + std::to_string(i), "TimeLimit", 0);
    ;
    ActivationCost = reader->ReadInt32("Buff-" + std::to_string(i), "ActivationCost", 0);

    Stats = new Stats();
    Stats[Stat::MaxAC] = reader->ReadByte("Buff-" + std::to_string(i), "BuffAc", 0);
    Stats[Stat::MaxMAC] = reader->ReadByte("Buff-" + std::to_string(i), "BuffMAC", 0);
    Stats[Stat::MaxDC] = reader->ReadByte("Buff-" + std::to_string(i), "BuffDc", 0);
    Stats[Stat::MaxMC] = reader->ReadByte("Buff-" + std::to_string(i), "BuffMc", 0);
    Stats[Stat::MaxSC] = reader->ReadByte("Buff-" + std::to_string(i), "BuffSc", 0);
    Stats[Stat::HP] = reader->ReadInt32("Buff-" + std::to_string(i), "BuffMaxHp", 0);
    Stats[Stat::MP] = reader->ReadInt32("Buff-" + std::to_string(i), "BuffMaxMp", 0);
    Stats[Stat::MineRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffMineRate", 0);
    Stats[Stat::GemRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffGemRate", 0);
    Stats[Stat::FishRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffFishRate", 0);
    Stats[Stat::ExpRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffExpRate", 0);
    Stats[Stat::CraftRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffCraftRate", 0);
    Stats[Stat::SkillGainMultiplier] = reader->ReadByte("Buff-" + std::to_string(i), "BuffSkillRate", 0);
    Stats[Stat::HealthRecovery] = reader->ReadByte("Buff-" + std::to_string(i), "BuffHpRegen", 0);
    Stats[Stat::SpellRecovery] = reader->ReadByte("Buff-" + std::to_string(i), "BuffMpRegen", 0);
    Stats[Stat::AttackBonus] = reader->ReadByte("Buff-" + std::to_string(i), "BuffAttack", 0);
    Stats[Stat::ItemDropRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffDropRate", 0);
    Stats[Stat::GoldDropRatePercent] = reader->ReadByte("Buff-" + std::to_string(i), "BuffGoldRate", 0);
}

void GuildBuffInfo::Save(InIReader *reader, int i)
{
    reader->Write("Buff-" + std::to_string(i), "Id", Id);
    reader->Write("Buff-" + std::to_string(i), "Icon", Icon);
    reader->Write("Buff-" + std::to_string(i), "Name", name);
    reader->Write("Buff-" + std::to_string(i), "LevelReq", LevelRequirement);
    reader->Write("Buff-" + std::to_string(i), "PointsReq", PointsRequirement);
    reader->Write("Buff-" + std::to_string(i), "TimeLimit", TimeLimit);
    reader->Write("Buff-" + std::to_string(i), "ActivationCost", ActivationCost);
    reader->Write("Buff-" + std::to_string(i), "BuffAc", Stats[Stat::MaxAC]);
    reader->Write("Buff-" + std::to_string(i), "BuffMAC", Stats[Stat::MaxMAC]);
    reader->Write("Buff-" + std::to_string(i), "BuffDc", Stats[Stat::MaxDC]);
    reader->Write("Buff-" + std::to_string(i), "BuffMc", Stats[Stat::MaxMC]);
    reader->Write("Buff-" + std::to_string(i), "BuffSc", Stats[Stat::MaxSC]);
    reader->Write("Buff-" + std::to_string(i), "BuffMaxHp", Stats[Stat::HP]);
    reader->Write("Buff-" + std::to_string(i), "BuffMaxMp", Stats[Stat::MP]);
    reader->Write("Buff-" + std::to_string(i), "BuffMineRate", Stats[Stat::MineRatePercent]);
    reader->Write("Buff-" + std::to_string(i), "BuffGemRate", Stats[Stat::GemRatePercent]);
    reader->Write("Buff-" + std::to_string(i), "BuffFishRate", Stats[Stat::FishRatePercent]);
    reader->Write("Buff-" + std::to_string(i), "BuffExpRate", Stats[Stat::ExpRatePercent]);
    ;
    reader->Write("Buff-" + std::to_string(i), "BuffCraftRate", Stats[Stat::CraftRatePercent]);
    reader->Write("Buff-" + std::to_string(i), "BuffSkillRate", Stats[Stat::SkillGainMultiplier]);
    reader->Write("Buff-" + std::to_string(i), "BuffHpRegen", Stats[Stat::HealthRecovery]);
    reader->Write("Buff-" + std::to_string(i), "BuffMpRegen", Stats[Stat::SpellRecovery]);
    reader->Write("Buff-" + std::to_string(i), "BuffAttack", Stats[Stat::AttackBonus]);
    reader->Write("Buff-" + std::to_string(i), "BuffDropRate", Stats[Stat::ItemDropRatePercent]);
    reader->Write("Buff-" + std::to_string(i), "BuffGoldRate", Stats[Stat::GoldDropRatePercent]);
}

void GuildBuffInfo::Save(BinaryWriter *writer)
{
    writer->Write(Id);
    writer->Write(Icon);
    writer->Write(name);
    writer->Write(LevelRequirement);
    writer->Write(PointsRequirement);
    writer->Write(TimeLimit);
    writer->Write(ActivationCost);

    Stats->Save(writer);
}

std::string GuildBuffInfo::ToString()
{
    return StringHelper::formatSimple("{0}: {1}", Id, name);
}

std::string GuildBuffInfo::ShowStats()
{
    std::string text = "";

    for (auto val : *Stats)
    {
        auto c = val->second->Value < 0 ? "Decreases" : "Increases";

//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
        auto txt = std::string::Format("{0} {1} by: {2}{3}.\n", c, val->second.Key, val->second->Value, (val->second.Key->ToString().Contains("Percent") ? "%" : ""));

        text += txt;
    }

    return text;
}

bool GuildBuff::getUsingGuildSkillIcon() const
{
    return Info != nullptr && Info->Icon < 1000;
}

GuildBuff::GuildBuff()
{
}

GuildBuff::GuildBuff(BinaryReader *reader)
{
    Id = reader->ReadInt32();
    Active = reader->ReadBoolean();
    ActiveTimeRemaining = reader->ReadInt32();
}

void GuildBuff::Save(BinaryWriter *writer)
{
    writer->Write(Id);
    writer->Write(Active);
    writer->Write(ActiveTimeRemaining);
}

GuildBuffOld::GuildBuffOld()
{
}

GuildBuffOld::GuildBuffOld(BinaryReader *reader)
{
    reader->ReadByte();
    reader->ReadInt64();
}
